package cn.acac.yygh.order.service.impl;

import cn.acac.yygh.enums.PaymentStatusEnum;
import cn.acac.yygh.model.order.OrderInfo;
import cn.acac.yygh.model.order.PaymentInfo;
import cn.acac.yygh.order.mapper.PaymentMapper;
import cn.acac.yygh.order.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {

    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer status) {
        // 首先根据订单id查询paymentInfo表是否有支付信息
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderInfo.getId());
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count > 0){
            // 代表数据库中已经有了此订单的支付信息，无需再创建新的
            return;
        }
        // 创建此订单的支付信息
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderId(orderInfo.getId());  // 设置订单id
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());   // 设置订单交易号
        paymentInfo.setPaymentType(status);     // 支付类型
        paymentInfo.setTotalAmount(orderInfo.getAmount());  // 支付金额
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus()); // 支付状态
        // 将数据保存到支付表中
        baseMapper.insert(paymentInfo);
    }
}
